package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 套餐业务实现
 */
@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    /**
     * 新增套餐，同时需要保存套餐和菜品的关联关系
     *
     * @param setmealDTO
     */
    @Transactional
    public void saveWithDish(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);

        //向套餐表插入数据
        setmealMapper.insert(setmeal);

        //获取生成的套餐id
        Long setmealId = setmeal.getId();

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealId);
        });

        //保存套餐和菜品的关联关系
        setmealDishMapper.insertBatch(setmealDishes);
    }

    /**
     * 分页查询
     *
     * @param setmealPageQueryDTO
     * @return
     */
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        int pageNum = setmealPageQueryDTO.getPage();
        int pageSize = setmealPageQueryDTO.getPageSize();

        PageHelper.startPage(pageNum, pageSize);
        Page<SetmealVO> page = setmealMapper.pageQuery(setmealPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());

    }

    @Override
    public void setStatus(Integer status, Long id) {
        Setmeal setmeal = Setmeal.builder().build();

        setmeal.setStatus(status);
        setmeal.setId(id);

        setmealMapper.updateById(setmeal);
    }

    @Override
    public SetmealVO findById(Long id) {
        Setmeal setmeal = setmealMapper.getById(id);
        SetmealVO setmealVO = SetmealVO.builder().build();
        BeanUtils.copyProperties(setmeal, setmealVO);
        if (null != setmeal) {
            Long setmealId = setmeal.getId();
            List<SetmealDish> dishes = setmealDishMapper.getBySetmealId(setmealId);
            setmealVO.setSetmealDishes(dishes);
        }
        return setmealVO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void setSetmeal(SetmealVO setmealVO) {

        Setmeal setmeal = Setmeal.builder().build();
        BeanUtils.copyProperties(setmealVO, setmeal);
        Long setmealId = setmeal.getId();

        setmealMapper.updateById(setmeal);
        setmealDishMapper.deleteBySetmealId(setmealId);

        List<SetmealDish> dishList = setmealVO.getSetmealDishes();
        for (SetmealDish setmealDish : dishList) {
            setmealDish.setSetmealId(setmealId);
        }
        setmealDishMapper.insertBatch(dishList);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteById(List<Long> ids) {

        for (Long id : ids) {
            Setmeal setmeal = setmealMapper.getById(id);
            if (setmeal.getStatus() == 1) {
                System.out.println("在售");
                return;
            }
            System.out.println("不在售");
            if (null != setmealDishMapper.getBySetmealId(id)){
                System.out.println("存在菜品");
                setmealDishMapper.deleteBySetmealId(id);
            }

            setmealMapper.deleteById(id);

        }

    }

    @Override
    public List<Setmeal> list(Setmeal setmeal) {

        List<Setmeal> setmealList = setmealMapper.getByCategoryId(setmeal);

        return setmealList;
    }
}

